package models

import (
	"database/sql"
	"fmt"
	"gORM/db"
)

type User struct {
	ID int  //ID默认主键(Id?),也可以自己设定`gorm:"primary_key"`
	Name string `gorm:"not null;type:varchar(25)"`
	//sql.NullInt32 实现了 Scanner/Valuer 接口,避免在查询时因为空值没有查询。指针也可以
	Age sql.NullInt32
	// 或 *int
	//Age *int
	//重设列名
	//Name string  `gorm:"column:user_name"` 将列名设为user_name
	Profile *Profile
}

type Profile struct {
	ID int
	User User `gorm:"foreignkey:UserID;AssociationForeignKey:Name"`
	UserID int
	Email string
}

type Language struct {
	ID int
	Name string
	User []*User `gorm:"many2many:user_languages;"`
}

func init() {
	err := db.InitDB()
	if err != nil {
		fmt.Println("db.InitDB",err)
	}

	//db.Db.CreateTable(&User{})
	db.Db.AutoMigrate(&User{},&Profile{},&Language{})
}
